{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "9e68cfbb",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "f92f4032",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. url \n",
    "base_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "\n",
    "# 2. api账户/通行证\n",
    "API_key = 'ZHfPcJzmzX_EdU6JQAnXU2s9rLQxXcD8'\n",
    "API_sercret = 'EM8ZGZ8gmpSD4aqAGTZ2EQgNCP8-v2AR'\n",
    "\n",
    "# 3. POST\n",
    "\n",
    "# 4. 所有API KEY 都可以调用本 API。\n",
    "#    其中calculate_all 和 face_rectangle 参数只有正式API key 才能使用，试用API Key 无法使用。\n",
    "\n",
    "# 5. payload: 酬载，必要阅读api文档中的*必读项* 和 *可选项*，及*参数说明*"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "88776645",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 2. 人脸分析（试调用analyze接口）\n",
    "# 1.准备base url (对应该功能的url链接)\n",
    "face_detect_url = \"https://api-cn.faceplusplus.com/facepp/v3/detect\"\n",
    "\n",
    "# 2. 准备参数数据\n",
    "payload = {\n",
    "    'api_key':'ZHfPcJzmzX_EdU6JQAnXU2s9rLQxXcD8',\n",
    "    'api_secret':'EM8ZGZ8gmpSD4aqAGTZ2EQgNCP8-v2AR',\n",
    "    'image_url':'https://img0.baidu.com/it/u=1456453464,2315522827&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=334',\n",
    "    'return_landmark':1,\n",
    "    'return_attributes':'gender,age,smiling,headpose'\n",
    "}\n",
    "# 3. 检查调用的请求方法：post\n",
    "# 4. 请求人脸数据\n",
    "r = requests.post(url = face_detect_url,params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "e0870e2f",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "99363426",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "8d05b8f1",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'r_analyze' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-20-17767aa2310d>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mpd\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mjson_normalize\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mr_analyze\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mjson\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'faces'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mT\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m: name 'r_analyze' is not defined"
     ]
    }
   ],
   "source": [
    "pd.json_normalize(r_analyze.json()['faces']).T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0685bb95",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 3. 人脸对比\n",
    "# 1.准备base url (对应该功能的url链接)\n",
    "face_detect_url = \"https://api-cn.faceplusplus.com/facepp/v3/compare\"\n",
    "\n",
    "# 2. 准备参数数据\n",
    "payload = {\n",
    "    'api_key':'ZHfPcJzmzX_EdU6JQAnXU2s9rLQxXcD8',\n",
    "    'api_secret':'EM8ZGZ8gmpSD4aqAGTZ2EQgNCP8-v2AR',\n",
    "    'image_url':'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fitem%2F202006%2F15%2F20200615144245_hqgog.jpg&refer=http%3A%2F%2Fc-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1649938843&t=459da916cf88469123dcd4adb3a4c033',\n",
    "\n",
    "}\n",
    "# 3. 检查调用的请求方法：post\n",
    "# 4. 请求人脸数据\n",
    "r = requests.post(url = face_detect_url,params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "208c7bc4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647573196,efc3b603-d8d3-49da-a593-d37b676bdd74',\n",
       " 'time_used': 237,\n",
       " 'faces': [{'face_token': '6a15064c6e3528a74556d51bde5abb57',\n",
       "   'face_rectangle': {'top': 79, 'left': 257, 'width': 153, 'height': 153},\n",
       "   'landmark': {'contour_chin': {'x': 304, 'y': 236},\n",
       "    'contour_left1': {'x': 257, 'y': 122},\n",
       "    'contour_left2': {'x': 257, 'y': 136},\n",
       "    'contour_left3': {'x': 257, 'y': 149},\n",
       "    'contour_left4': {'x': 260, 'y': 163},\n",
       "    'contour_left5': {'x': 266, 'y': 176},\n",
       "    'contour_left6': {'x': 273, 'y': 189},\n",
       "    'contour_left7': {'x': 279, 'y': 202},\n",
       "    'contour_left8': {'x': 285, 'y': 215},\n",
       "    'contour_left9': {'x': 292, 'y': 229},\n",
       "    'contour_right1': {'x': 401, 'y': 107},\n",
       "    'contour_right2': {'x': 404, 'y': 126},\n",
       "    'contour_right3': {'x': 404, 'y': 146},\n",
       "    'contour_right4': {'x': 402, 'y': 166},\n",
       "    'contour_right5': {'x': 395, 'y': 185},\n",
       "    'contour_right6': {'x': 383, 'y': 202},\n",
       "    'contour_right7': {'x': 366, 'y': 215},\n",
       "    'contour_right8': {'x': 347, 'y': 226},\n",
       "    'contour_right9': {'x': 326, 'y': 234},\n",
       "    'left_eye_bottom': {'x': 271, 'y': 123},\n",
       "    'left_eye_center': {'x': 270, 'y': 120},\n",
       "    'left_eye_left_corner': {'x': 261, 'y': 121},\n",
       "    'left_eye_lower_left_quarter': {'x': 266, 'y': 123},\n",
       "    'left_eye_lower_right_quarter': {'x': 276, 'y': 121},\n",
       "    'left_eye_pupil': {'x': 272, 'y': 119},\n",
       "    'left_eye_right_corner': {'x': 280, 'y': 119},\n",
       "    'left_eye_top': {'x': 270, 'y': 117},\n",
       "    'left_eye_upper_left_quarter': {'x': 265, 'y': 119},\n",
       "    'left_eye_upper_right_quarter': {'x': 275, 'y': 117},\n",
       "    'left_eyebrow_left_corner': {'x': 258, 'y': 101},\n",
       "    'left_eyebrow_lower_left_quarter': {'x': 263, 'y': 101},\n",
       "    'left_eyebrow_lower_middle': {'x': 268, 'y': 100},\n",
       "    'left_eyebrow_lower_right_quarter': {'x': 273, 'y': 100},\n",
       "    'left_eyebrow_right_corner': {'x': 279, 'y': 99},\n",
       "    'left_eyebrow_upper_left_quarter': {'x': 262, 'y': 98},\n",
       "    'left_eyebrow_upper_middle': {'x': 267, 'y': 96},\n",
       "    'left_eyebrow_upper_right_quarter': {'x': 274, 'y': 96},\n",
       "    'mouth_left_corner': {'x': 279, 'y': 187},\n",
       "    'mouth_lower_lip_bottom': {'x': 300, 'y': 201},\n",
       "    'mouth_lower_lip_left_contour1': {'x': 288, 'y': 193},\n",
       "    'mouth_lower_lip_left_contour2': {'x': 283, 'y': 195},\n",
       "    'mouth_lower_lip_left_contour3': {'x': 289, 'y': 201},\n",
       "    'mouth_lower_lip_right_contour1': {'x': 319, 'y': 188},\n",
       "    'mouth_lower_lip_right_contour2': {'x': 328, 'y': 188},\n",
       "    'mouth_lower_lip_right_contour3': {'x': 315, 'y': 197},\n",
       "    'mouth_lower_lip_top': {'x': 299, 'y': 193},\n",
       "    'mouth_right_corner': {'x': 338, 'y': 177},\n",
       "    'mouth_upper_lip_bottom': {'x': 297, 'y': 181},\n",
       "    'mouth_upper_lip_left_contour1': {'x': 289, 'y': 175},\n",
       "    'mouth_upper_lip_left_contour2': {'x': 281, 'y': 180},\n",
       "    'mouth_upper_lip_left_contour3': {'x': 287, 'y': 183},\n",
       "    'mouth_upper_lip_right_contour1': {'x': 302, 'y': 173},\n",
       "    'mouth_upper_lip_right_contour2': {'x': 320, 'y': 173},\n",
       "    'mouth_upper_lip_right_contour3': {'x': 316, 'y': 177},\n",
       "    'mouth_upper_lip_top': {'x': 296, 'y': 175},\n",
       "    'nose_contour_left1': {'x': 283, 'y': 118},\n",
       "    'nose_contour_left2': {'x': 276, 'y': 140},\n",
       "    'nose_contour_left3': {'x': 280, 'y': 156},\n",
       "    'nose_contour_lower_middle': {'x': 289, 'y': 159},\n",
       "    'nose_contour_right1': {'x': 304, 'y': 115},\n",
       "    'nose_contour_right2': {'x': 307, 'y': 139},\n",
       "    'nose_contour_right3': {'x': 301, 'y': 156},\n",
       "    'nose_left': {'x': 273, 'y': 151},\n",
       "    'nose_right': {'x': 312, 'y': 150},\n",
       "    'nose_tip': {'x': 282, 'y': 146},\n",
       "    'right_eye_bottom': {'x': 329, 'y': 113},\n",
       "    'right_eye_center': {'x': 329, 'y': 110},\n",
       "    'right_eye_left_corner': {'x': 316, 'y': 114},\n",
       "    'right_eye_lower_left_quarter': {'x': 322, 'y': 114},\n",
       "    'right_eye_lower_right_quarter': {'x': 336, 'y': 111},\n",
       "    'right_eye_pupil': {'x': 329, 'y': 109},\n",
       "    'right_eye_right_corner': {'x': 343, 'y': 108},\n",
       "    'right_eye_top': {'x': 327, 'y': 105},\n",
       "    'right_eye_upper_left_quarter': {'x': 320, 'y': 108},\n",
       "    'right_eye_upper_right_quarter': {'x': 335, 'y': 106},\n",
       "    'right_eyebrow_left_corner': {'x': 301, 'y': 95},\n",
       "    'right_eyebrow_lower_left_quarter': {'x': 313, 'y': 90},\n",
       "    'right_eyebrow_lower_middle': {'x': 325, 'y': 85},\n",
       "    'right_eyebrow_lower_right_quarter': {'x': 338, 'y': 85},\n",
       "    'right_eyebrow_right_corner': {'x': 350, 'y': 88},\n",
       "    'right_eyebrow_upper_left_quarter': {'x': 310, 'y': 85},\n",
       "    'right_eyebrow_upper_middle': {'x': 324, 'y': 79},\n",
       "    'right_eyebrow_upper_right_quarter': {'x': 338, 'y': 80}},\n",
       "   'attributes': {'gender': {'value': 'Female'},\n",
       "    'age': {'value': 22},\n",
       "    'smile': {'value': 98.361, 'threshold': 50.0},\n",
       "    'headpose': {'pitch_angle': -6.181054,\n",
       "     'roll_angle': -9.124897,\n",
       "     'yaw_angle': 21.321772}}}],\n",
       " 'image_id': 'cPQOa1a2UeDVcpuSVsfqSQ==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "0b607c48",
   "metadata": {},
   "outputs": [],
   "source": [
    "guo_face01 = ''\n",
    "guo_face02 = ''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "b5d4deeb",
   "metadata": {},
   "outputs": [],
   "source": [
    "face_analyze_url = 'https://api-cn.faceplusplus.com/facepp/v3/compare'\n",
    "payload = {\n",
    "    'api_key':'ZHfPcJzmzX_EdU6JQAnXU2s9rLQxXcD8',\n",
    "    'api_secret':'EM8ZGZ8gmpSD4aqAGTZ2EQgNCP8-v2AR',\n",
    "    \n",
    "}\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6d0652a8",
   "metadata": {},
   "source": [
    "# 4. 人脸检测函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "5aced4d4",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "API_key:'ZHfPcJzmzX_EdU6JQAnXU2s9rLQxXcD8'\n",
    "API_secret:'EM8ZGZ8gmpSD4aqAGTZ2EQgNCP8-v2AR'\n",
    "image_url:'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fitem%2F202006%2F15%2F20200615144245_hqgog.jpg&refer=http%3A%2F%2Fc-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1649938843&t=459da916cf88469123dcd4adb3a4c033'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "31c2e06d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 思考：能否将一些功能作函数的封装，实现更加便捷的api调用\n",
    "def face_detect(API_key,API_secret,image_url,landmark=None,attributes=None,face_rectangle=None,beauty_score_min=0,beauty_score_max=100):\n",
    "    \"\"\"face++ 人脸检测接口函数(图片链接)\"\"\"\n",
    "    url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "    payload={\n",
    "        'api_key':API_key,\n",
    "        'api_secret':API_secret,\n",
    "        'image_url':image_url,\n",
    "        'return_landmark':landmark,\n",
    "        'return_attributes':attributes,\n",
    "        'face_rectangle':face_rectangle,\n",
    "        'beauty_score_min':beauty_score_min,\n",
    "        'beauty_score_max':beauty_score_max   \n",
    "    }\n",
    "    r = requests.post(url,payload)\n",
    "    return r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "48323e39",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'API_key' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-10-c6334c75ce7c>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;31m# 调用函数\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mface_detect\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mAPI_key\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mAPI_secret\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mimage_url\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m: name 'API_key' is not defined"
     ]
    }
   ],
   "source": [
    "# 调用函数\n",
    "face_detect(API_key,API_secret,image_url)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a7585df7",
   "metadata": {},
   "source": [
    "# 人脸搜索"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "b17e6868",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1.url\n",
    "face_rectangle_url ='https://api-cn.faceplusplus.com/facepp/v3/search'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "0068b4b8",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 2. api账户/通行证\n",
    "API_key:'ZHfPcJzmzX_EdU6JQAnXU2s9rLQxXcD8'\n",
    "API_secret:'EM8ZGZ8gmpSD4aqAGTZ2EQgNCP8-v2AR'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "d3034ef7",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 3.post\n",
    "payload = {\n",
    "    'api_key':'ZHfPcJzmzX_EdU6JQAnXU2s9rLQxXcD8',\n",
    "    'api_secret':'EM8ZGZ8gmpSD4aqAGTZ2EQgNCP8-v2AR',\n",
    "    'image_url':'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fitem%2F202006%2F15%2F20200615144245_hqgog.jpg&refer=http%3A%2F%2Fc-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1649938843&t=459da916cf88469123dcd4adb3a4c033',\n",
    "    'outer_id':'huau1'\n",
    "}\n",
    "r = requests.post(face_rectangle_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "2ebbf14e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<bound method Response.json of <Response [400]>>"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "04e32b2f",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d7ded66f",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8704897c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
